/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sirtet.tetris.program;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import sirtet.tetris.TetrisPiece;
import sirtet.tetris.TetrisPieceFactory;
import sirtet.tetris.generator.TetrisPieceGenerator;

/**
 * Main program that checks the pieces output by another program
 * @author guangwei.zhu
 */
public class PieceChecker {

    /**
     * Main entry of the program.
     * @param args Two arguments are expected from command-line.  The first
     * argument indicates the number of squares in each piece, and the second
     * argument is the path of the reference file.
     */
    public static void main(String[] args)
            throws IOException, FileNotFoundException {

        int numsquare = Integer.valueOf(args[0]);
        File file = new File(args[1]);
        String originalstr;

        TetrisPieceFactory factory = new TetrisPieceFactory();

        List<TetrisPiece> pieces = TetrisPieceGenerator.enumerate(numsquare);
        List<TetrisPiece> filepieces = factory.fromFile(file);
        ArrayList<String> extras = new ArrayList<String>();

        boolean isFound;    // If a piece is matched

        for (TetrisPiece refpiece : filepieces) {
            isFound = false;
            originalstr = refpiece.toString();  // Store a copy
            refpiece.normalize();               // Normalize to make comparable
            for (TetrisPiece piece : pieces) {
                if (piece.equals(refpiece)) {
                    pieces.remove(piece);       // A match piece is removed
                    isFound = true;
                    break;
                }
            }

            if (!isFound) {
                extras.add(originalstr);        // A piece not found in the list
            }
        }

        // Print result
        if (!pieces.isEmpty()) {
            System.out.println("Missing pieces: " + pieces.size());
            for (TetrisPiece piece : pieces) {
                System.out.println(piece);
            }
        }
        if (!extras.isEmpty()) {
            System.out.println("Extra pieces: " + extras.size());
            for (String str : extras) {
                System.out.println(str);
            }
        }
    }
}
